package com.starsea.book.ui.newpage.pop

import android.content.Context
import android.graphics.Color
import android.widget.TextView
import com.blankj.utilcode.util.SpanUtils
import com.lxj.xpopup.core.CenterPopupView
import com.starsea.book.R
import com.starsea.book.ui.newpage.entity.Today
import com.starsea.book.ui.newpage.normalview.RadiusTextView
import com.starsea.book.ui.newpage.utils.finialDouble

/**
 * @author Lee
 * @createTime 2023-08-04 16 GMT+8
 * @desc :
 */
class WelfareSignPop(
    var mContext: Context,
    var today: Today,
    var welfareData: MutableList<Today>,
    var onResult: ((today: Today) -> Unit)? = null
) :
    CenterPopupView(mContext) {

    lateinit var rtv_img_1: RadiusTextView
    lateinit var rtv_img_2: RadiusTextView
    lateinit var rtv_img_3: RadiusTextView
    lateinit var rtv_img_4: RadiusTextView
    lateinit var rtv_img_5: RadiusTextView
    lateinit var rtv_img_6: RadiusTextView
    lateinit var rtv_img_7: RadiusTextView
    lateinit var rtv_img_10: RadiusTextView
    lateinit var rtv_img_14: RadiusTextView

    lateinit var rtv_text_1: RadiusTextView
    lateinit var rtv_text_2: RadiusTextView
    lateinit var rtv_text_3: RadiusTextView
    lateinit var rtv_text_4: RadiusTextView
    lateinit var rtv_text_5: RadiusTextView
    lateinit var rtv_text_6: RadiusTextView
    lateinit var rtv_text_7: RadiusTextView
    lateinit var rtv_text_10: RadiusTextView
    lateinit var rtv_text_14: RadiusTextView

    lateinit var rtv_dot_1: RadiusTextView
    lateinit var rtv_dot_2: RadiusTextView
    lateinit var rtv_dot_3: RadiusTextView
    lateinit var rtv_dot_4: RadiusTextView
    lateinit var rtv_dot_5: RadiusTextView
    lateinit var rtv_dot_6: RadiusTextView
    lateinit var rtv_dot_7: RadiusTextView
    lateinit var rtv_dot_10: RadiusTextView
    lateinit var rtv_dot_14: RadiusTextView

    lateinit var rtv_line_1: RadiusTextView
    lateinit var rtv_line_2: RadiusTextView
    lateinit var rtv_line_3: RadiusTextView
    lateinit var rtv_line_4: RadiusTextView
    lateinit var rtv_line_5: RadiusTextView
    lateinit var rtv_line_6: RadiusTextView
    lateinit var rtv_line_7: RadiusTextView
    lateinit var rtv_line_10: RadiusTextView
    lateinit var rtv_line_14: RadiusTextView
    lateinit var rtv_line_right: RadiusTextView

    lateinit var rtv_day_1: RadiusTextView
    lateinit var rtv_day_2: RadiusTextView
    lateinit var rtv_day_3: RadiusTextView
    lateinit var rtv_day_4: RadiusTextView
    lateinit var rtv_day_5: RadiusTextView
    lateinit var rtv_day_6: RadiusTextView
    lateinit var rtv_day_7: RadiusTextView
    lateinit var rtv_day_10: RadiusTextView
    lateinit var rtv_day_14: RadiusTextView

    var listText: MutableList<RadiusTextView> = arrayListOf()
    var listDay: MutableList<RadiusTextView> = arrayListOf()
    var listDot: MutableList<RadiusTextView> = arrayListOf()
    var listLine: MutableList<RadiusTextView> = arrayListOf()
    var listImg: MutableList<RadiusTextView> = arrayListOf()


    override fun getImplLayoutId(): Int {
        return R.layout.layout_welfare_sign_pop
    }

    override fun onCreate() {
        super.onCreate()

        rtv_img_1 = findViewById(R.id.rtv_img_1)
        rtv_img_2 = findViewById(R.id.rtv_img_2)
        rtv_img_3 = findViewById(R.id.rtv_img_3)
        rtv_img_4 = findViewById(R.id.rtv_img_4)
        rtv_img_5 = findViewById(R.id.rtv_img_5)
        rtv_img_6 = findViewById(R.id.rtv_img_6)
        rtv_img_7 = findViewById(R.id.rtv_img_7)
        rtv_img_10 = findViewById(R.id.rtv_img_10)
        rtv_img_14 = findViewById(R.id.rtv_img_14)
        listImg.add(rtv_img_1)
        listImg.add(rtv_img_2)
        listImg.add(rtv_img_3)
        listImg.add(rtv_img_4)
        listImg.add(rtv_img_5)
        listImg.add(rtv_img_6)
        listImg.add(rtv_img_7)
        listImg.add(rtv_img_10)
        listImg.add(rtv_img_14)


        rtv_text_1 = findViewById(R.id.rtv_text_1)
        rtv_text_2 = findViewById(R.id.rtv_text_2)
        rtv_text_3 = findViewById(R.id.rtv_text_3)
        rtv_text_4 = findViewById(R.id.rtv_text_4)
        rtv_text_5 = findViewById(R.id.rtv_text_5)
        rtv_text_6 = findViewById(R.id.rtv_text_6)
        rtv_text_7 = findViewById(R.id.rtv_text_7)
        rtv_text_10 = findViewById(R.id.rtv_text_10)
        rtv_text_14 = findViewById(R.id.rtv_text_14)

        listText.add(rtv_text_1)
        listText.add(rtv_text_2)
        listText.add(rtv_text_3)
        listText.add(rtv_text_4)
        listText.add(rtv_text_5)
        listText.add(rtv_text_6)
        listText.add(rtv_text_7)
        listText.add(rtv_text_10)
        listText.add(rtv_text_14)



        rtv_dot_1 = findViewById(R.id.rtv_dot_1)
        rtv_dot_2 = findViewById(R.id.rtv_dot_2)
        rtv_dot_3 = findViewById(R.id.rtv_dot_3)
        rtv_dot_4 = findViewById(R.id.rtv_dot_4)
        rtv_dot_5 = findViewById(R.id.rtv_dot_5)
        rtv_dot_6 = findViewById(R.id.rtv_dot_6)
        rtv_dot_7 = findViewById(R.id.rtv_dot_7)
        rtv_dot_10 = findViewById(R.id.rtv_dot_8)
        rtv_dot_14 = findViewById(R.id.rtv_dot_9)
        listDot.add(rtv_dot_1)
        listDot.add(rtv_dot_2)
        listDot.add(rtv_dot_3)
        listDot.add(rtv_dot_4)
        listDot.add(rtv_dot_5)
        listDot.add(rtv_dot_6)
        listDot.add(rtv_dot_7)
        listDot.add(rtv_dot_10)
        listDot.add(rtv_dot_14)

        rtv_line_1 = findViewById(R.id.rtv_line_1)
        rtv_line_2 = findViewById(R.id.rtv_line_2)
        rtv_line_3 = findViewById(R.id.rtv_line_3)
        rtv_line_4 = findViewById(R.id.rtv_line_4)
        rtv_line_5 = findViewById(R.id.rtv_line_5)
        rtv_line_6 = findViewById(R.id.rtv_line_6)
        rtv_line_7 = findViewById(R.id.rtv_line_7)

        rtv_line_10 = findViewById(R.id.rtv_line_8)
        rtv_line_14 = findViewById(R.id.rtv_line_9)
        rtv_line_right = findViewById(R.id.rtv_line_right)

        listLine.add(rtv_line_1)
        listLine.add(rtv_line_2)
        listLine.add(rtv_line_3)
        listLine.add(rtv_line_4)
        listLine.add(rtv_line_5)
        listLine.add(rtv_line_6)
        listLine.add(rtv_line_7)
        listLine.add(rtv_line_10)
        listLine.add(rtv_line_14)

        rtv_day_1 = findViewById(R.id.rtv_day_1)
        rtv_day_2 = findViewById(R.id.rtv_day_2)
        rtv_day_3 = findViewById(R.id.rtv_day_3)
        rtv_day_4 = findViewById(R.id.rtv_day_4)
        rtv_day_5 = findViewById(R.id.rtv_day_5)
        rtv_day_6 = findViewById(R.id.rtv_day_6)
        rtv_day_7 = findViewById(R.id.rtv_day_7)
        rtv_day_10 = findViewById(R.id.rtv_day_10)
        rtv_day_14 = findViewById(R.id.rtv_day_14)

        listDay.add(rtv_day_1)
        listDay.add(rtv_day_2)
        listDay.add(rtv_day_3)
        listDay.add(rtv_day_4)
        listDay.add(rtv_day_5)
        listDay.add(rtv_day_6)
        listDay.add(rtv_day_7)
        listDay.add(rtv_day_10)
        listDay.add(rtv_day_14)


        val welfareLocalData: MutableList<Today> = arrayListOf()


        welfareData.forEachIndexed { index, today ->

            if (index != 7 && index != 8 && index != 12 && index != 11 && index != 10) {
                welfareLocalData.add(today)
            }

        }

        val find = welfareData.find { it.time == today.time }

        val todayIndex = welfareData.indexOf(find)

        listDot.forEachIndexed { index, radiusTextView ->

            when (todayIndex) {
                7, 8 -> {
                    radiusTextView.isSelected = index < 7
                    rtv_line_right.isSelected = true
                }

                10, 11, 12 -> {
                    radiusTextView.isSelected = index < 8
                    rtv_line_right.isSelected = true
                }

                5 -> {
                    radiusTextView.isSelected = index < todayIndex
                    rtv_line_right.isSelected = true
                }

                else -> {
                    radiusTextView.isSelected = index < todayIndex
                }
            }


        }

        listLine.forEachIndexed { index, radiusTextView ->

            when (todayIndex) {
                7, 8 -> {
                    radiusTextView.isSelected = index < 7
                }

                10, 11, 12 -> {
                    radiusTextView.isSelected = index < 8
                }

                else -> {
                    radiusTextView.isSelected = index < todayIndex
                }
            }
        }


        listImg.forEachIndexed { index, radiusTextView ->

            when (todayIndex) {
                7, 8 -> {
                    radiusTextView.isSelected = index < 7
                }

                10, 11, 12 -> {
                    radiusTextView.isSelected = index < 8
                }

                else -> {
                    radiusTextView.isSelected = index < todayIndex
                }
            }


        }

        listText.forEachIndexed { index, radiusTextView ->

            when (todayIndex) {
                7, 8 -> {
                    radiusTextView.isSelected = index < 7
                }

                10, 11, 12 -> {
                    radiusTextView.isSelected = index < 8
                }

                else -> {
                    radiusTextView.isSelected = index < todayIndex
                }
            }


        }


        welfareLocalData.forEachIndexed { index, today ->

            listText[index].text = (today.amount ?: "0").toDouble().finialDouble()
            listDay[index].text = "${(today.time ?: "0").toDouble().finialDouble()}天"


        }


        val tv_sign = findViewById<TextView>(R.id.tv_sign)

//        是否已看广告 1、是 2、否
        if (today.adver == 1) {
            tv_sign.text = SpanUtils().append("已签到").create()
        } else {
            tv_sign.text = SpanUtils().append("看视频再")
                .append("+${(today.amount ?: "0").toDouble().finialDouble()}金币")
                .setForegroundColor(Color.parseColor("#E23737")).create()
        }

        tv_sign.setOnClickListener {
            if (today.adver == 1) {
                dismiss()
            } else {
                onResult!!.invoke(today)
                dismiss()
            }
        }

    }


}